package top.arryweb.application.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.arryweb.application.common.constant.KeyName;
import top.arryweb.application.common.exception.BusinessRuntimeException;
import top.arryweb.application.domain.dto.CreateCapitalBlotterDTO;
import top.arryweb.application.domain.pojo.BookKeepCapitalBlotter;
import top.arryweb.application.domain.pojo.BookKeepCapitalDaily;
import top.arryweb.application.domain.pojo.BookKeepPaymentOrder;
import top.arryweb.application.domain.pojo.BookKeepTradeBlotter;
import top.arryweb.application.mapper.BookKeepCapitalBlotterMapper;
import top.arryweb.application.mapper.BookKeepTradeBlotterMapper;
import top.arryweb.application.service.IBookKeepCapitalBlotterService;
import top.arryweb.application.service.IBookKeepTradeBlotterService;

/**
 * <p>
 * 资金流水表 服务实现类
 * </p>
 *
 * @author Mac
 * @since 2021-05-28
 */
@Service
public class BookKeepCapitalBlotterServiceImpl extends
    ServiceImpl<BookKeepCapitalBlotterMapper, BookKeepCapitalBlotter> implements
    IBookKeepCapitalBlotterService {

  @Autowired
  private RedisTemplate<String, Object> redisTemplate;

  @Autowired(required = false)
  private BookKeepCapitalBlotterMapper capitalBlotterMapper;
  /**
   * 获取资金流水ID
   */
  @Override
  public Long getCapitalBlotterId(CreateCapitalBlotterDTO dto) {
    HashOperations<String, Long, BookKeepPaymentOrder> payOrderOperations = redisTemplate
        .opsForHash();
    HashOperations<String, Long, BookKeepTradeBlotter> tradeBlotterOperations = redisTemplate
        .opsForHash();
    HashOperations<String, Long, BookKeepCapitalDaily> capitalDailyOperations = redisTemplate
        .opsForHash();

    BookKeepCapitalDaily capitalDaily = Optional.ofNullable(capitalDailyOperations
        .get(KeyName.ENTITY_CAPITAL_DAILY_TYPE.getName(), dto.getCapitalDailyTypeId()))
        .orElseThrow(() -> new BusinessRuntimeException("未找到相应的资金类型"));
    BookKeepTradeBlotter tradeBlotter = Optional.ofNullable(
        tradeBlotterOperations.get(KeyName.ENTITY_TRADE_BLOTTER.getName(), dto.getTradeBlotterId()))
            .orElseThrow(() -> new BusinessRuntimeException("未找到相应的订单流水"));
    BookKeepPaymentOrder paymentOrder = Optional
        .ofNullable(payOrderOperations.get(KeyName.ENTITY_ORDER
            .getName(), dto.getOrderId()))
        .orElseThrow(() -> new BusinessRuntimeException("未找到相应的交易流水"));

    BookKeepCapitalBlotter capitalBlotter = new BookKeepCapitalBlotter(dto);
    /*capitalBlotter.setCapitalDailyTapeId(dto.getCapitalDailyTypeId());
    capitalBlotter.setTradeBlotterId(dto.getTradeBlotterId());
    capitalBlotter.setOrderId(dto.getOrderId());*/
    capitalBlotter.setCapitalDailyTapeId(capitalDaily.getId());
    capitalBlotter.setTradeBlotterId(tradeBlotter.getId());
    capitalBlotter.setOrderId(paymentOrder.getId());
    capitalBlotter.setVersion(1L);

    capitalBlotterMapper.insert(capitalBlotter);
    putRedis(capitalBlotter);
    return capitalBlotter.getId();
  }

  void putRedis(BookKeepCapitalBlotter capitalBlotter){
    HashOperations<String, Long, BookKeepCapitalBlotter> operations = redisTemplate.opsForHash();
    operations.put(KeyName.ENTITY_CAPITAL.getName(), capitalBlotter.getId(), capitalBlotter);

    HashOperations<String, Long, List<Long>> idsOperations = redisTemplate.opsForHash();

    List<Long> tradeCapitalIdList = Optional.ofNullable(
        idsOperations.get(KeyName.IDS_TRADE_CAPITAL.getName(), capitalBlotter.getTradeBlotterId())).orElse(new ArrayList<>());
    tradeCapitalIdList.add(capitalBlotter.getId());
    idsOperations.put(KeyName.IDS_TRADE_CAPITAL.getName(), capitalBlotter.getTradeBlotterId(),
        tradeCapitalIdList);

    List<Long> orderCapitalIdList = Optional.ofNullable(idsOperations.get(KeyName.IDS_ORDER_CAPITAL.getName(), capitalBlotter.getOrderId()))
        .orElse(new ArrayList<>());
    orderCapitalIdList.add(capitalBlotter.getId());
    idsOperations.put(KeyName.IDS_ORDER_CAPITAL.getName(),capitalBlotter.getOrderId(),orderCapitalIdList);
  }
}
